package com.zw.mizhe.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by zhangwei on 16/10/21.
 */

public class AppUtils {

    /**
     * 是否第一次运行app
     *
     * @param context
     * @return
     */
    public static boolean isFristRun(Context context) {
        SharedPreferences sp = context.getSharedPreferences("frist", Context.MODE_PRIVATE);
        boolean isFristRun = sp.getBoolean("isFristRun", true);
        int spVersion = sp.getInt("spVersion", 0);
        int versionCode = getVersionCode(context);
        if (spVersion != 0 && spVersion != versionCode || isFristRun) {
            SharedPreferences.Editor edit = sp.edit();
            edit.putBoolean("isFristRun", false);
            edit.putInt("version", versionCode);
            edit.apply();
            return true;
        }
        return false;
    }


    public static int getVersionCode(Context context) {
        int version = 0;
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            version = (int) Double.parseDouble(packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }


}
